package com.axios.controller.servlets;

import com.axios.dao.impl.JsonServerDAOImpl;
import com.axios.dao.interfaces.JsonServerDAO;
import com.axios.models.pojo.JsonServer;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/delJsonServer")
public class JsonServerDelServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应头允许跨域
        response.setHeader("Access-Control-Allow-Origin","*");

        // 响应头
        response.setHeader("Access-Control-Allow-Headers","*");

        // 接收 ajax 发送过来的参数
        String id = request.getParameter("id");
        System.out.print("id:"+id);
        // 创建接口的实现类
        JsonServerDAO jsonServerDAO = new JsonServerDAOImpl();

        // 创建一个对象
        JsonServer jsonServer = new JsonServer();
        jsonServer.setId(Integer.parseInt(id));

        // 执行删除操作
        int count = jsonServerDAO.del(jsonServer);

        // 判断是否删除成功
        if(count == -1){
            response.getWriter().write("删除失败！");
            System.out.print(1);
        }else{
            response.getWriter().write("删除成功！");
            System.out.print(2);
        }
    }
}
